package com.project_test.kun.travelling_1st.Discovery;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Cache;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.project_test.kun.travelling_1st.Discovery.discover_detail_beans.discover_detail_detail_bean;
import com.project_test.kun.travelling_1st.Discovery.discover_detail_beans.discover_detail_detail_pict_bean;
import com.project_test.kun.travelling_1st.Discovery.discover_detail_beans.discover_detail_good_bean;
import com.project_test.kun.travelling_1st.Discovery.discover_detail_beans.discover_detail_item_bean;
import com.project_test.kun.travelling_1st.R;
import com.project_test.kun.travelling_1st.Utils.CustomVolley;
import com.project_test.kun.travelling_1st.Utils.LListView;
import com.project_test.kun.travelling_1st.beans.Flag;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class Discover_Detail_Activity extends AppCompatActivity implements View.OnClickListener {
    private String detailPath;
    private TextView item_detail_title, item_detail_title_tel, detail_detail_recommend;
    private ImageView item_detail_head_pic, discover_item_finish;
    private LListView item_detail_good_list, item_detail_detail_list;
    private RequestQueue mRequestQueue;
    private JSONObject originalJson;
    private LinearLayout discover_detail_tel_layout;
    private discover_detail_item_bean mDiscover_detail_item_bean;
    private List<discover_detail_detail_bean> details;
    private List<discover_detail_detail_bean> detailsCopy = new ArrayList<>();
    private List<discover_detail_good_bean> goods;
    private Item_detail_detail_list_Adapter mAdapter;
    private Item_detail_good_list_Adapter mGoodAdapter;
    private List<Flag> mData = new ArrayList<>();
    private TextView orderBar;

    Handler hander = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if (msg.what == 1) {
                originalJson = (JSONObject) msg.obj;
                parseJson(originalJson);
                insertData();
                details = mDiscover_detail_item_bean.getDetails();
                for (int i = 0; i < details.size(); i++) {
                    Flag flag = new Flag();
                    flag.setFlag2(R.mipmap.ic_expandable_view_chevron);
                    mData.add(flag);
                }

                for (int i = 0; i < details.size(); i++) {
                    discover_detail_detail_bean bean = new discover_detail_detail_bean();
                    List<discover_detail_detail_pict_bean> picts = new ArrayList<>();
                    for (int i1 = 0; i1 < details.get(i).getPicts().size(); i1++) {
                        discover_detail_detail_pict_bean bean2 = new discover_detail_detail_pict_bean();
                        bean2.setPic_url(details.get(i).getPicts().get(i1).getPic_url());
                        bean2.setContent(details.get(i).getPicts().get(i1).getContent());
                        picts.add(bean2);
                    }
                    bean.setPicts(picts);
                    detailsCopy.add(bean);
                }
                goods = mDiscover_detail_item_bean.getGoods();
                mAdapter.notifyDataSetChanged();
                mGoodAdapter.notifyDataSetChanged();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discover__detail_);
        detailPath = getIntent().getStringExtra("detailPath");
        details = new ArrayList<>();
        goods = new ArrayList<>();
        mAdapter = new Item_detail_detail_list_Adapter();
        mGoodAdapter = new Item_detail_good_list_Adapter();
        jsonRequest(detailPath);
        initView();
        discover_item_finish.setOnClickListener(this);
    }

    private void initView() {
        discover_item_finish = (ImageView) findViewById(R.id.discover_item_finish);
        item_detail_title = (TextView) findViewById(R.id.item_detail_title);
        item_detail_title_tel = (TextView) findViewById(R.id.item_detail_tel);
        item_detail_head_pic = (ImageView) findViewById(R.id.item_detail_head_pic);
        item_detail_detail_list = (LListView) findViewById(R.id.item_detail_detail_list);
        item_detail_good_list = (LListView) findViewById(R.id.item_detail_good_list);
        detail_detail_recommend = (TextView) findViewById(R.id.detail_detail_recommend);
        discover_detail_tel_layout = (LinearLayout) findViewById(R.id.discover_detail_tel_layout);
        orderBar= (TextView) findViewById(R.id.order_bar);
    }

    private void insertData() {
        item_detail_title.setText(mDiscover_detail_item_bean.getTitle());
        if (mDiscover_detail_item_bean.getTel() != null) {
            if (mDiscover_detail_item_bean.getTel().length() >= 1) {
                item_detail_title_tel.setText(mDiscover_detail_item_bean.getTel());
            } else {
                discover_detail_tel_layout.setVisibility(View.GONE);
            }
        }
        if (mDiscover_detail_item_bean.getGoods() != null) {
            if (mDiscover_detail_item_bean.getGoods().size() == 0) {
                detail_detail_recommend.setVisibility(View.GONE);
                orderBar.setVisibility(View.VISIBLE);
            }else {
                orderBar.setVisibility(View.GONE);

            }
        }


        String path = mDiscover_detail_item_bean.getHead_pic();
        if (path != null) {
            Picasso.with(this).load(path).into(item_detail_head_pic);
        }
        item_detail_detail_list.setAdapter(mAdapter);
        item_detail_good_list.setAdapter(mGoodAdapter);
    }


    private void jsonRequest(final String path) {
        mRequestQueue = CustomVolley.getInstance(this).sRequestQueue;
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(JsonObjectRequest.Method.GET,
                path, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Message msg = new Message();
                msg.what = 1;
                msg.obj = response;
                hander.sendMessage(msg);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //            获取缓存
                Cache cache = mRequestQueue.getCache();
                if (cache != null) {
                    Cache.Entry entry = cache.get(path);
                    if (entry != null) {
                        byte[] data = entry.data;
                        if (data != null) {
                            try {
                                String result = new String(data, "UTF-8");
                                JSONObject obj = new JSONObject(result);
                                Message msg = new Message();
                                msg.obj = obj;
                                hander.sendMessage(msg);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        });
        mRequestQueue.add(jsonObjectRequest);
    }

    private void parseJson(JSONObject jsonObject) {
        try {
            JSONObject obj1 = jsonObject.optJSONObject("data");
            mDiscover_detail_item_bean = new discover_detail_item_bean();
            mDiscover_detail_item_bean.parseJson(obj1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        finish();
    }

    private class Item_detail_good_list_Adapter extends BaseAdapter {

        @Override
        public int getCount() {
            return goods == null ? 0 : goods.size();
        }

        @Override
        public Object getItem(int position) {
            return goods.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View ret = convertView;
            LayoutInflater inflater = LayoutInflater.from(getBaseContext());
            ret = inflater.inflate(R.layout.discover_detail_good_item_layout, parent, false);
            GHolder gHolder = (GHolder) ret.getTag();
            if (gHolder == null) {
                gHolder = new GHolder(ret);
                ret.setTag(gHolder);
            }
            gHolder.bindView(position, goods.get(position));
            return ret;
        }

        private class GHolder {
            private TextView detail_good_item_title, detail_good_item_rank, detail_good_item_price, detail_good_item_addr;
            private ImageView detail_good_item_head_pic;

            public GHolder(View itemView) {
                detail_good_item_title = (TextView) itemView.findViewById(R.id.detail_good_item_title);
                detail_good_item_rank = (TextView) itemView.findViewById(R.id.detail_good_item_rank);
                detail_good_item_price = (TextView) itemView.findViewById(R.id.detail_good_item_price);
                detail_good_item_addr = (TextView) itemView.findViewById(R.id.detail_good_item_addr);
                detail_good_item_head_pic = (ImageView) itemView.findViewById(R.id.detail_good_item_head_pic);
            }

            public void bindView(int i, discover_detail_good_bean item) {
                detail_good_item_title.setText(item.getTitle());
                detail_good_item_rank.setText(item.getScores());
                detail_good_item_price.setText("¥" + item.getPrice() + "元/晚");
                detail_good_item_addr.setText(item.getAddress());
                String path = item.getHead_pic();
                Picasso.with(getBaseContext()).load(path).into(detail_good_item_head_pic);
            }
        }
    }

    private class Item_detail_detail_list_Adapter extends BaseAdapter {
        private List<discover_detail_detail_bean> mDetails;

        public Item_detail_detail_list_Adapter() {
            mDetails = details;
        }

        @Override
        public void notifyDataSetChanged() {
            mDetails = details;
            super.notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mDetails == null ? 0 : mDetails.size();
        }

        @Override
        public Object getItem(int position) {
            return mDetails.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View ret = convertView;
            LayoutInflater inflater = LayoutInflater.from(getBaseContext());
            ret = inflater.inflate(R.layout.discover_detail_detail_item_layout, parent, false);
            Holder holder = (Holder) ret.getTag();
            if (holder == null) {
                holder = new Holder(ret);
                ret.setTag(holder);
            }
            holder.bindView(position, mDetails.get(position));
            return ret;
        }

        private class Holder {
            private TextView detail_detail_title;
            private ImageView detail_detail_title_expend_icon;
            private TextView detail_detail_title_expend_text;
            private LListView detail_detail_pict_list;
//            private ObjectAnimator rotate1, rotate2;
            private List<discover_detail_detail_pict_bean> picts;
            private Item_detail_pict_list_Adapter mPictAdapter;

            public Holder(View itemView) {
                detail_detail_title_expend_icon = (ImageView) itemView.findViewById(R.id.detail_detail_title_expend_icon);
                detail_detail_title_expend_text = (TextView) itemView.findViewById(R.id.detail_detail_title_expend_text);
                detail_detail_title = (TextView) itemView.findViewById(R.id.detail_detail_title);
                detail_detail_pict_list = (LListView) itemView.findViewById(R.id.detail_detail_pict_list);
//                rotate1 = ObjectAnimator.ofFloat(detail_detail_title_expend_icon, "rotation", 0, -180);
//                rotate1.setDuration(200);
//                rotate2 = ObjectAnimator.ofFloat(detail_detail_title_expend_icon, "rotation", -180, -360);
//                rotate2.setDuration(200);
            }

            public void bindView(final int i, final discover_detail_detail_bean item) {
                detail_detail_title_expend_icon.setImageResource(mData.get(i).getFlag2());
                detail_detail_title.setText(item.getTag_title());
                picts = item.getPicts();
                mPictAdapter = new Item_detail_pict_list_Adapter(picts);
                detail_detail_pict_list.setAdapter(mPictAdapter);
                detail_detail_title_expend_text.setText(mData.get(i).getFlag());
                View.OnClickListener listener = new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String texttmp = detail_detail_title_expend_text.getText().toString();
                        if ("收起".equals(texttmp)) {
                            mData.get(i).setFlag("展开");
                            mData.get(i).setFlag2(R.mipmap.ic_expandable_view_chevron2);
                            details.get(i).getPicts().clear();
                            mAdapter.notifyDataSetChanged();
//                            rotate1.start();
                        }
                        if ("展开".equals(texttmp)) {
                            mData.get(i).setFlag("收起");
                            mData.get(i).setFlag2(R.mipmap.ic_expandable_view_chevron);
                            details.get(i).getPicts().addAll(detailsCopy.get(i).getPicts());
                            mAdapter.notifyDataSetChanged();
//                            rotate2.start();
                        }
                    }
                };
                detail_detail_title_expend_text.setOnClickListener(listener);
            }
        }

        private class Item_detail_pict_list_Adapter extends BaseAdapter {
            private List<discover_detail_detail_pict_bean> mPicts;

            public Item_detail_pict_list_Adapter(List<discover_detail_detail_pict_bean> picts) {
                mPicts = picts;
            }

            @Override
            public int getCount() {
                return mPicts == null ? 0 : mPicts.size();
            }

            @Override
            public Object getItem(int position) {
                return mPicts.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View ret = convertView;
                LayoutInflater inflater = LayoutInflater.from(getBaseContext());
                ret = inflater.inflate(R.layout.discover_detail_pict_item_layout, parent, false);
                PHolder holder = (PHolder) ret.getTag();
                if (holder == null) {
                    holder = new PHolder(ret);
                    ret.setTag(holder);
                }
                holder.bindView(position, mPicts.get(position));
                return ret;
            }

            private class PHolder {
                private TextView detail_detail_pict_content;
                private ImageView detail_detail_pict_head_pic;

                public PHolder(View itemView) {
                    detail_detail_pict_head_pic = (ImageView) itemView.findViewById(R.id.detail_detail_pict_head_pic);
                    detail_detail_pict_content = (TextView) itemView.findViewById(R.id.detail_detail_pict_content);
                }

                public void bindView(int i, discover_detail_detail_pict_bean item) {
                    detail_detail_pict_content.setText(item.getContent());
                    String path = item.getPic_url();
                    if (path != null) {
                        if (path.length() > 0) {
                            Picasso.with(getBaseContext()).load(path).into(detail_detail_pict_head_pic);
                        }
                    }
                }
            }
        }
    }
}
